<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 微信第三方登录
 */
class Weixin {
    private $appId = "wx13e6f0926f65da1d";//微信应用id
    private $appSecret = "974cc253f66718871fea2b07f05c89fe";//微信应用密钥
    public function __construct() {
        //通过引用的方式赋给变量来初始化原始的CodeIgniter对象
    }
    /**
     * 获取code
     * @param type $config 相关配置
     */
    function get_code($config){
        $url = "https://open.weixin.qq.com/connect/qrconnect?appid=".$this->appId."&redirect_uri=".urlencode($config['redirectUri'])."&response_type=code&scope=snsapi_login&state=".$config['state']."#wechat_redirect";
        header("Location:".$url);
    }
    /**
     * 获取access_token
     * @param type $code
     */
    function get_access_token($code){
        $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$this->appId."&secret=".$this->appSecret."&code=".$code."&grant_type=authorization_code";
        $result = file_get_contents($url);
        return $result;
    }
    
    function get_userinfo($access_token,$open_id){
        $url = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$open_id";
        $result = file_get_contents($url);
        return $result;
    }
}
